package com.xiaoshuidi.cloud.module.iot.task;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.contract.api.ContractApi;
import com.xiaoshuidi.cloud.module.contract.vo.RentOrderVO;
import com.xiaoshuidi.cloud.module.contract.vo.RenterVO;
import com.xiaoshuidi.cloud.module.iot.common.ResultModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.ammeter.AmmeterSwitchOnOffReqVO;
import com.xiaoshuidi.cloud.module.iot.dal.model.ammeter.AmmeterSwitchOnOffResVO;
import com.xiaoshuidi.cloud.module.iot.dal.model.doorLock.AddPasswordModel;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDoorlockPassword;
import com.xiaoshuidi.cloud.module.iot.dal.repository.DeviceDosageRepository;
import com.xiaoshuidi.cloud.module.iot.dal.repository.TbDoorLockPasswordRepository;
import com.xiaoshuidi.cloud.module.iot.service.AmmeterService;
import com.xiaoshuidi.cloud.module.iot.service.DoorLockService;
import com.xiaoshuidi.cloud.module.iot.service.InteceptorService;
import com.xiaoshuidi.cloud.module.rooms.api.sdiot.IotRoomDeviceApi;
import com.xiaoshuidi.cloud.module.rooms.enums.SdiotWaringTypeEnum;
import com.xiaoshuidi.cloud.module.rooms.vo.RoomSdiotDeviceMappingVo;
import com.xiaoshuidi.cloud.module.rooms.vo.RoomSdiotWaringVo;
import com.xiaoshuidi.cloud.module.rooms.vo.RoomWaringInfoVo;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
/*
 * @description:预警定时任务
 * @author: James
 * @date: 2023/7/19 15:08
 * @param:
 * @return:
 **/
@Component
@Slf4j
@RequiredArgsConstructor
public class YinTongWaringTask {
    @Autowired
    private IotRoomDeviceApi iotRoomDeviceApi;
    @Autowired
    private DeviceDosageRepository deviceDosageDataAccess;
    @Autowired
    private ContractApi contractApi;
    @Autowired
    private AmmeterService ammeterService;
    @Autowired
    private DoorLockService doorLockService;
    @Autowired
    private TbDoorLockPasswordRepository doorLockPasswordDataAccess;
    @Autowired
    private InteceptorService inteceptorService;

    @XxlJob("yintong-waring-task")
    @TenantIgnore
    public void yinTongWaringTask() {
        //获取已逾期未支付账单的房间id
        CommonResult<List<RentOrderVO>> overdueUnpaidOrder = contractApi.getOverdueUnpaidOrder();
        List<RentOrderVO> roomIds = overdueUnpaidOrder.getCheckedData();
        List<Long> roomList = new ArrayList<>();
        for (RentOrderVO orderVO : roomIds) {
            roomList.add(orderVO.getRoomId());
        }
        log.info("已逾期账单房间id：" + JSONObject.toJSONString(roomList));
        //获取预警配置信息
        CommonResult<List<RoomSdiotWaringVo>> waringConfig = iotRoomDeviceApi.getWaringConfig();
        List<RoomSdiotWaringVo> checkedData = waringConfig.getCheckedData();
        //根据配置信息新增预警信息
        for (RoomSdiotWaringVo roomSdiotWaringVo : checkedData) {
            if (null != roomSdiotWaringVo.getDeviceNo()) {
                //判断是否开启用电量预警
                if (roomSdiotWaringVo.getAmmterWaring() && roomSdiotWaringVo.getDeviceType().equals("ammeter")) {
                    //根据设备编号和配置度数判断是否超预警
                    //设备编号查询用电量
                    Double oneDayRead = deviceDosageDataAccess.getOneDayRead(roomSdiotWaringVo.getDeviceNo());
                    if (null != oneDayRead) {
                        //超过则发送预警信息
                        if (oneDayRead.compareTo(roomSdiotWaringVo.getAmmterWaringRead()) > 0) {
                            RoomWaringInfoVo roomWaringInfoVo = new RoomWaringInfoVo();
                            roomWaringInfoVo.setWaringInfo("电量使用量预警");
                            roomWaringInfoVo.setWaringType(SdiotWaringTypeEnum.AMMETER_WARING.getValue());
                            roomWaringInfoVo.setDeviceNo(roomSdiotWaringVo.getDeviceNo());
                            roomWaringInfoVo.setRead(oneDayRead);
                            roomWaringInfoVo.setRoomId(roomSdiotWaringVo.getRoomId());
                            iotRoomDeviceApi.insertWaring(roomWaringInfoVo);
                        }
                    }
                }
                //判断是否开启用热水预警
                if (roomSdiotWaringVo.getHotWorterWaring() && roomSdiotWaringVo.getDeviceType().equals("hot_watermeter")) {
                    //根据设备编号和配置度数判断是否超预警
                    //设备编号查询用水量
                    Double oneDayRead = deviceDosageDataAccess.getOneDayRead(roomSdiotWaringVo.getDeviceNo());
                    if (null != oneDayRead) {
                        //超过则发送预警信息
                        if (oneDayRead.compareTo(roomSdiotWaringVo.getHotWorterWaringRead()) > 0) {
                            RoomWaringInfoVo roomWaringInfoVo = new RoomWaringInfoVo();
                            roomWaringInfoVo.setWaringInfo("热水使用量预警");
                            roomWaringInfoVo.setWaringType(SdiotWaringTypeEnum.HOT_WATER_WARING.getValue());
                            roomWaringInfoVo.setRead(oneDayRead);
                            roomWaringInfoVo.setDeviceNo(roomSdiotWaringVo.getDeviceNo());
                            roomWaringInfoVo.setRoomId(roomSdiotWaringVo.getRoomId());
                            iotRoomDeviceApi.insertWaring(roomWaringInfoVo);
                        }
                    }
                }
                //判断是否开启用冷水预警
                if (roomSdiotWaringVo.getColdWorterWaring() && roomSdiotWaringVo.getDeviceType().equals("cold_watermeter")) {
                    //根据设备编号和配置度数判断是否超预警
                    //设备编号查询用水量
                    Double oneDayRead = deviceDosageDataAccess.getOneDayRead(roomSdiotWaringVo.getDeviceNo());
                    if (null != oneDayRead) {
                        //超过则发送预警信息
                        if (oneDayRead.compareTo(roomSdiotWaringVo.getColdWorterWaringRead()) > 0) {
                            RoomWaringInfoVo roomWaringInfoVo = new RoomWaringInfoVo();
                            roomWaringInfoVo.setWaringInfo("冷水使用量预警");
                            roomWaringInfoVo.setWaringType(SdiotWaringTypeEnum.COLD_WATER_WARING.getValue());
                            roomWaringInfoVo.setRead(oneDayRead);
                            roomWaringInfoVo.setDeviceNo(roomSdiotWaringVo.getDeviceNo());
                            roomWaringInfoVo.setRoomId(roomSdiotWaringVo.getRoomId());
                            iotRoomDeviceApi.insertWaring(roomWaringInfoVo);
                        }
                    }
                }
                //判断是否开启电表智能催租
                if (roomSdiotWaringVo.getAmmeterPromotionWaring() && roomSdiotWaringVo.getDeviceType().equals("ammeter")) {
                    //开启智能催租预警并存在逾期未支付账单
                    for (Long roomid : roomList) {
                        if (String.valueOf(roomid).equals(String.valueOf(roomSdiotWaringVo.getRoomId()))) {
                            //断电
                            try {
                                RoomWaringInfoVo roomWaringInfoVo = new RoomWaringInfoVo();
                                roomWaringInfoVo.setWaringInfo("电表智能催租预警");
                                roomWaringInfoVo.setWaringType(SdiotWaringTypeEnum.AMMETER_URGE.getValue());
                                roomWaringInfoVo.setDeviceNo(roomSdiotWaringVo.getDeviceNo());
                                AmmeterSwitchOnOffReqVO ammeterSwitchOnOffReqVO = new AmmeterSwitchOnOffReqVO();
                                ammeterSwitchOnOffReqVO.setDeviceNo(roomSdiotWaringVo.getDeviceNo());
                                roomWaringInfoVo.setRoomId(roomSdiotWaringVo.getRoomId());
                                ResultModel<AmmeterSwitchOnOffResVO> ammeterSwitchOnOffResVOResultModel = ammeterService.setAmmeterSwitchOn(ammeterSwitchOnOffReqVO);
                                log.info("电表断电执行结果：" + JSONObject.toJSONString(ammeterSwitchOnOffResVOResultModel));
                                //添加操作记录
                                inteceptorService.addOperateRecord(roomSdiotWaringVo.getDeviceNo(),10);
                                iotRoomDeviceApi.insertWaring(roomWaringInfoVo);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                //判断是否开启门锁智能催租
                if (roomSdiotWaringVo.getDoorlockPromotionWaring() && roomSdiotWaringVo.getDeviceType().equals("doorlock")) {
                    //开启门锁智能催租预警并存在逾期未支付账单
                    for (Long roomid : roomList) {
                        if (String.valueOf(roomid).equals(String.valueOf(roomSdiotWaringVo.getRoomId()))) {
                            //根据device_no获取所有门锁信息
                            List<TbDoorlockPassword> byDeviceOrderBy = doorLockPasswordDataAccess.findByDeviceOrderBy(roomSdiotWaringVo.getDeviceNo());
                            log.info("根据device_no获取所有门锁信息：" + JSONObject.toJSONString(byDeviceOrderBy));
                            //冻结这个房间下所有门锁密码
                            for(TbDoorlockPassword tbDoorlockPassword:byDeviceOrderBy){
                                AddPasswordModel addPasswordModel = new AddPasswordModel();
                                addPasswordModel.setDeviceNo(roomSdiotWaringVo.getDeviceNo());
                                addPasswordModel.setThirdPassId(tbDoorlockPassword.getThirdPasswordId());
                                try {
                                    ResultModel<AddPasswordModel> addPasswordModelResultModel = doorLockService.unfrozenPassword(addPasswordModel);
                                    //添加操作记录
                                    inteceptorService.addOperateRecord(addPasswordModel.getDeviceNo(),5);
                                    log.info("冻结密码返回结果：" + JSONObject.toJSONString(addPasswordModelResultModel));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            //获取承租人并给承租人发送预警信息
                            CommonResult<RenterVO> contractRenterInfoByRoomId = null;
                            //冻结门锁
                            try {
                                contractRenterInfoByRoomId = contractApi.getContractRenterInfoByRoomId(roomSdiotWaringVo.getRoomId());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            log.info("承租人信息：" + JSONObject.toJSONString(contractRenterInfoByRoomId));
                            if (String.valueOf(contractRenterInfoByRoomId.getCode()).equals("0")) {
                                RenterVO idCheckedData = contractRenterInfoByRoomId.getCheckedData();
                                if (null != idCheckedData) {
                                    RoomWaringInfoVo roomWaringInfoVo = new RoomWaringInfoVo();
                                    roomWaringInfoVo.setWaringInfo("门锁智能催租预警");
                                    roomWaringInfoVo.setWaringType(SdiotWaringTypeEnum.DOOR_LOCK_URGE.getValue());
                                    roomWaringInfoVo.setDeviceNo(roomSdiotWaringVo.getDeviceNo());
                                    roomWaringInfoVo.setTenantName(idCheckedData.getCustomerName());
                                    roomWaringInfoVo.setTenantTel(idCheckedData.getCustomerPhone());
                                    roomWaringInfoVo.setRoomId(roomSdiotWaringVo.getRoomId());
                                    iotRoomDeviceApi.insertWaring(roomWaringInfoVo);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
